package com.wans.guli.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.wans.guli.product.constant.em.AttrTypeEnum;
import com.wans.guli.product.entity.ProductAttrValueEntity;
import com.wans.guli.product.vo.AttrRestVO;
import com.wans.guli.product.vo.ProductAttrValueVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wans.guli.product.entity.AttrEntity;
import com.wans.guli.product.service.AttrService;
import com.wans.guli.common.utils.PageUtils;
import com.wans.guli.common.utils.R;


/**
 * 商品属性
 *
 * @author wans
 * @email wansconst@163.com
 * @date 2025-08-01 15:14:35
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @GetMapping("/base/listforspu/{spuId}")
    public R list(@PathVariable("spuId") Long spuId,@RequestParam Map<String, Object> params){
        List<ProductAttrValueEntity> productAttrValueEntities = attrService.queryAttr(spuId,params);

        return R.ok().put("data", productAttrValueEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/base/list/{catId}")
    public R listBaseById(@PathVariable("catId") Long catId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPageByAttrId(catId, params, AttrTypeEnum.BASE.getCode());

        return R.ok().put("page", page);
    }
    @GetMapping("/sale/list/{catId}")
    public R listSaleById(@PathVariable("catId") Long catId, @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPageByAttrId(catId, params, AttrTypeEnum.SALE.getCode());

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrEntity attr = attrService.getInfoById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrRestVO attr) {
//        attrService.save(attr);
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody  AttrRestVO attr) {
        attrService.updateAttrById(attr);

        return R.ok();
    }

    @PostMapping("/update/{spuId}")
    public R update(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueVO> productAttrValueVOList) {


        attrService.updateBatchProductAttrValueById(spuId,productAttrValueVOList);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
